package com.zuo.service.serviceImpl;

import com.zuo.entity.Facekey;
import com.zuo.entity.User;
import com.zuo.mapper.UserMapper;
import com.zuo.service.UserService;
import com.zuo.util.DateFormat;
import com.zuo.util.MD5;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/1 17:09
 */
@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        User login = userMapper.login(username, MD5.md5WithSalt(password));
        return login;
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    @Override
    public List<User> listUser() {
        List<User> userList = userMapper.listUser();
        for (User user:userList){
            user.setcTime(DateFormat.format(user.getCreateTime()));
        }
        return userList;
    }

    @Override
    public List<User> searchUser(String username, int status) {
        List<User> userList = userMapper.searchUser(username,status);
        for (User user:userList){
            user.setcTime(DateFormat.format(user.getCreateTime()));
        }
        return userList;
    }

    @Override
    public User getOne(int id) {
        return userMapper.getOne(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)//修改的要加异常回滚
    public int updateUser(User user) {
        user.setPassword(MD5.md5WithSalt(user.getPassword()));
        return userMapper.updateUser(user);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int removeUsers(String ids) {
        String[] id = ids.split(",");
        int count=0;
        for (String i : id) {
            int uid=0;
            if(StringUtils.isNotBlank(i)){
                uid=Integer.parseInt(i);
            }
            int result = userMapper.removeUser(uid);
            if(result!=0){
                count++;
            }
        }
        if(count!=id.length){
            throw new RuntimeException("删除失败");
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int addUser(User user) {
        user.setCreateTime(new Date(System.currentTimeMillis()));
        user.setPassword(MD5.md5WithSalt(user.getPassword()));
        return userMapper.addUser(user);
    }

    @Override
    public Facekey getFaceKey() {
        return userMapper.getFaceKey();
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateFaceUrlByName(String faceUrl,String facePath,String username) {
        return userMapper.updateFaceUrlByName(faceUrl,facePath,username);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updatePassword(String username, String oldPassword, String newPassword) {
        oldPassword = MD5.md5WithSalt(oldPassword);
        newPassword = MD5.md5WithSalt(newPassword);
        return userMapper.updatePassword(username,oldPassword,newPassword);
    }
}
